<template>
    <div>
        <div style="padding: 1px 0;">
          <el-input
            style="width: 200px; font-size:medium"
            prefix-icon="el-icon-present"
            placeholder="请输入鲜花名称"
            v-model="name"
          ></el-input>
          <el-input
            style="width: 200px; font-size:medium"
            prefix-icon="el-icon-notebook-2"
            placeholder="请输入鲜花码"
            class="ml-5"
            v-model="code"
          ></el-input>
          <el-button class="ml-5" type="primary" @click="load" style="font-size:medium">搜索</el-button>
          <el-button class="ml-5" type="info" @click="reset" style="font-size:medium">重置</el-button>
        </div>

        <div>
          <div style="padding: 5px 0; float:right; margin-top:-30px">
            <el-button type="warning" @click="handleAdd" style="font-size:medium"><span style="font-size:20px">添加</span></el-button>
            <el-popconfirm
                  class="ml-5"
                  confirm-button-text='确定'
                  cancel-button-text='我再想想'
                  icon="el-icon-info"
                  icon-color="red"
                  title="您确定要批量删除吗?"
                  @confirm="handleDeleteBatch">
            <el-button type="danger" slot="reference" style="font-size:medium"><span style="font-size:20px">批量删除</span></el-button>
          </el-popconfirm>
            <el-button class="ml-5" type="primary" @click="exp" style="font-size:medium"><span style="font-size:20px">导出</span></el-button>
          </div>
      </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg2'" @selection-change="handleSelectionChange" style="font-size:large; ">
          <el-table-column type="selection" width="40"></el-table-column>
          <el-table-column prop="name" label="鲜花名称" width="130" align="center"></el-table-column>
          <el-table-column prop="price" label="鲜花价格" width="108" align="center">
            <template v-slot="scope">
              {{ scope.row.price }}.00
            </template>
          </el-table-column>
          <el-table-column prop="code" label="鲜花码" width="170" align="center"></el-table-column>
          <el-table-column label="图片" prop="img" width="180" align="center">
            <template slot-scope="scope">
              <el-image style="width:90px; height:90px" :src="scope.row.img" :preview-src-list="[scope.row.img]">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="createUser" label="创建人" width="110" align="center"></el-table-column>
          <el-table-column prop="sales" label="销量" width="110" align="center">
              <template v-slot="scope">
                 <el-tag type="warning" size="medium"><span style="font-size:17px; font-weight:600">{{ scope.row.sales }}</span></el-tag>
              </template>
          </el-table-column>
          <el-table-column label="启售/禁售" width="110" align="center">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" @change="changeStatus(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="223" align="center">
            <template slot-scope="scope">
              <el-button type="success" @click="handleEdit(scope.row)" style="font-size:medium">编辑 <i class="el-icon-edit"></i></el-button>
              <el-popconfirm
                class="ml-5"
                confirm-button-text='确定删除'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要删除吗?"
                @confirm="handleDel(scope.row.id)"
                >
              <el-button type="danger" slot="reference"  style="font-size:medium">删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <div style="padding: 10px 0; margin-left:240px; margin-top:10px;">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[4, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>

        <el-dialog title="鲜花信息" :visible.sync="dialogFormVisible" width="35%">
          <el-form label-width="90px" size="small">
            <el-form-item label="鲜花名称：">
              <el-input width="50%" v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="鲜花价格：">
              <el-input v-model="form.price" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="鲜花码：">
              <el-input v-model="form.code" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="鲜花花语: ">
              <el-input v-model="form.description" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="适用对象: ">
              <el-input v-model="form.targets" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="鲜花组成: ">
              <el-input v-model="form.constitute" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="图片: ">
              <el-upload
                action="http://8.138.58.18:7777/file/upload"
                ref="uploadImg"
                :on-success="handleImgUploadSuccess"
                clearFiles
                >
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer" style="margin-top:-20px">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
  name: 'Flower',
  data(){
    return{
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 4,
        name: "",
        code: "",
        dialogFormVisible: false,
        multipleSelection: [],
        form:{},
        isEdit: false,
        vavava: true,
    }
  },
  created(){
    this.load();
    // this.changeStatus();
  },
  methods:{
    load() {
        this.request.get("/flower/page",{
          params :{
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.name,
            code: this.code
          }
        }).then(res =>{
          console.log('没没没事',res);
          this.tableData = res.records
          this.total = res.total
        })
      },
      clearFlile(){
        this.$refs.uploadImg.clearFiles();
      },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum;
      this.load();
    },
    reset() {
       this.name = "",
       this.code = "",
       this.load()
    },
    handleAdd(){
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row){
      console.log('row',row);
      this.form = Object.assign({},row)
      this.dialogFormVisible = true
      this.isEdit = true
    },
    handleDel(id){
      console.log('id',id);
      this.request.delete("/flower/delete/"+id).then(res =>{
        if(res){
          this.$message.success('删除鲜花成功!')
          this.load()
        }else{
          this.$message.error('删除鲜花失败!')
          this.load()
        }
      })
    },
    handleDeleteBatch(){
      let ids = this.multipleSelection.map(v => v.id)
      console.log('ids',ids)
      if(ids.length === 0){
        this.$message.warning('批量删除鲜花前,请先勾选若干鲜花')
      }else{
        this.request.delete("/flower/deleteBatch/",{data:ids}).then(res =>{
          if(res.code === '200'){
            this.$message.success('批量删除鲜花成功!')
            this.load()
          }else{
            this.$message.error('批量删除鲜花失败!')
            this.load()
          }
        })
      }
    },
    handleSelectionChange(val){
      this.multipleSelection = val
    },
    save(){
      if(this.isEdit === false){
        console.log('add');
        this.request.post("/flower/save",this.form).then(res =>{
        console.log(res)
        if(res){
          console.log('西瓜');
          this.$message.success('添加鲜花成功!')
          this.dialogFormVisible = false
          this.clearFlile();
          this.load()
        }else{
          this.$message.error('添加鲜花失败!')
          this.dialogFormVisible = false
          this.load()
        }
      })
      }else{
        console.log('edit');
        this.request.put("/flower/edit",this.form).then((res) =>{
          if(res){
            this.$message.success("修改鲜花信息成功!")
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }else{
            this.$message.error('修改鲜花信息失败!')
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }
        })
      }
    },
    cancel(){
      this.dialogFormVisible = false
      this.load()
    },
    exp(){
      window.open("http://8.138.58.18:7777/flower/export")
    },
    changeStatus(row){
      this.request.put('/flower/edit',row).then(res =>{
        if(res.code === '200'){
          this.$notify.success('修改鲜花状态成功！')
          this.load();
        }
      })
    },
    changeEnable(){
      console.log('启售');
    },
    handleImgUploadSuccess(res,file,files){
      console.log('草莓 ',res);
      console.log('西瓜 ',file);
      console.log('香蕉 ',files);
      this.form.img = res
    }
  }
};
</script>

<style>
.headerBg2 {
  background-color: rgb(223, 202, 171) !important;
}
</style>